package com.fmn.dots_final_i_hope;

import model.GameField;
import model.Line;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	ImageView[][] gameField;


	private TextView player1Name; 
	private TextView player2Name;
	private TextView player1Score;
	private TextView player2Score;
	private ImageView player1Image;
	private ImageView player2Image;

	private final String SCORE_STRING = "Score: ";
	private Activity act = this;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setLayout();
		initializeFields();		
	}


	private void initializeFields() {
		gameField = new ImageView[PlayerControl.choosenCol * 2 + 1][PlayerControl.choosenRow * 2 + 1];
		GameField.createField(this, gameField);
		GameField.setReferences(gameField);
		GameField.setLineListeners(gameField, linePressListener);

		PlayerControl.setMaxPointsPossible();
		
		  player1Name = (TextView) findViewById(R.id.p1Name);
		  player2Name = (TextView) findViewById(R.id.p2Name);
		  player1Score = (TextView) findViewById(R.id.p1Score);
		  player2Score = (TextView) findViewById(R.id.p2Score);
		  player1Image = (ImageView) findViewById(R.id.p1Image);
		  player2Image = (ImageView) findViewById(R.id.p2Image);
		 player1Name.setText(PlayerControl.p1.getName());
		 player2Name.setText(PlayerControl.p2.getName());
		 
		 player1Image.setImageResource(PlayerControl.p1.getChosenImage());
		 player2Image.setImageResource(PlayerControl.p2.getChosenImage());
		int p1CurScore = PlayerControl.p1.getCurrentPoints();
		int p2CurScore = PlayerControl.p2.getCurrentPoints();
		player1Score.setText(SCORE_STRING+p1CurScore);
		player2Score.setText(SCORE_STRING+p2CurScore);
		
		
	}


	private void setLayout() {
		if (PlayerControl.choosenCol == 3 && PlayerControl.choosenRow == 3) {
			setContentView(R.layout.field_3x3);
		} else if (PlayerControl.choosenCol == 4
				&& PlayerControl.choosenRow == 3) {
			setContentView(R.layout.field_4x3);

		} else if (PlayerControl.choosenCol == 4
				&& PlayerControl.choosenRow == 4) {
			setContentView(R.layout.field_4x4);

		} else if (PlayerControl.choosenCol == 5
				&& PlayerControl.choosenRow == 4) {
			setContentView(R.layout.field_5x4);

		} else if (PlayerControl.choosenCol == 5
				&& PlayerControl.choosenRow == 5) {
			setContentView(R.layout.field_5x5);

		}
	}

	/**
	 * Setting the main thing that plays the game - touch action on the lines
	 */

	View.OnTouchListener linePressListener = new View.OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Line line = (Line) v;

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
					PlayerControl.doAPlayerTurn(line);
					showWhichPlayerTurnIs();
					updateScoreTexts();

				if (PlayerControl.checkForEndGame())
						GameDialogs.showEndGameDialog(MainActivity.this, act);

				return true;
			}
			return false;
		}
	};


	private void updateScoreTexts() {
		player1Score.setText(SCORE_STRING+PlayerControl.p1.getCurrentPoints());
		player2Score.setText(SCORE_STRING+PlayerControl.p2.getCurrentPoints());
	}


	private void showWhichPlayerTurnIs() {
		 Animation zoomInAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoomin);
		 Animation zoomOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoomout);
		 zoomInAnimation.setFillEnabled(true);
		 zoomInAnimation.setFillAfter(true);
		 zoomOutAnimation.setFillEnabled(true);
		 zoomOutAnimation.setFillAfter(true);
		 
		if(PlayerControl.getCurrentPlayer().equals(PlayerControl.p1)){
			player1Image.startAnimation(zoomInAnimation);
			player2Image.startAnimation(zoomOutAnimation);
			player1Name.setShadowLayer(20, 0, 0, getResources().getColor(R.color.white));
			player2Name.setShadowLayer(0, 0, 0, getResources().getColor(R.color.white));
		}
		if(PlayerControl.getCurrentPlayer().equals(PlayerControl.p2)){
			player2Image.startAnimation(zoomInAnimation);
			player1Image.startAnimation(zoomOutAnimation);
			player2Name.setShadowLayer(20, 0, 0, getResources().getColor(R.color.white));
			player1Name.setShadowLayer(0, 0, 0, getResources().getColor(R.color.white));
		}
	}
	@Override
	protected void onStop() {
		PlayerControl.nullStaticVariables();
		super.onStop();
	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
